
package com.jf.cloud.live.service.impl;

import cn.hutool.core.date.DateUtil;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.live.param.LiveUsableNumParam;
import com.jf.cloud.live.constant.LiveInterfaceType;
import com.jf.cloud.live.dto.LiveLogDTO;
import com.jf.cloud.live.mapper.LiveLogMapper;
import com.jf.cloud.live.model.LiveLog;
import com.jf.cloud.live.service.LiveLogService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 直播记录
 *
 * @author zz
 * @date 2022-08-17 17:09:05
 */
@Service
public class LiveLogServiceImpl implements LiveLogService {

    @Autowired
    private LiveLogMapper liveLogMapper;

    @Override
    public PageVO<LiveLog> page(PageDTO pageDTO, LiveLogDTO liveLogDTO) {
        return PageUtil.doPage(pageDTO, () -> liveLogMapper.list(liveLogDTO));
    }

    @Override
    public LiveLog getByLiveLogId(Long liveLogId) {
        return liveLogMapper.getByLiveLogId(liveLogId);
    }

    @Override
    public void save(LiveLog liveLog) {
        liveLogMapper.save(liveLog);
    }

    @Override
    public void update(LiveLog liveLog) {
        liveLogMapper.update(liveLog);
    }

    @Override
    public void deleteById(Long liveLogId) {
        liveLogMapper.deleteById(liveLogId);
    }

    @Override
    public LiveUsableNumParam getLiveNum(Long shopId, LiveInterfaceType type) {
        LiveLogDTO liveLogDTO = new LiveLogDTO();
        liveLogDTO.setStartTime(DateUtil.beginOfDay(new Date()));
        liveLogDTO.setEndTime(DateUtil.endOfDay(new Date()));
        liveLogDTO.setType(type.value());
        liveLogDTO.setShopId(shopId);
        List<LiveLog> liveLogs = liveLogMapper.list(liveLogDTO);
        LiveUsableNumParam liveUsableNumParam = new LiveUsableNumParam();
        int platformNum = 0;
        int shopNum = 0;
        if(CollectionUtils.isNotEmpty(liveLogs)){
            for (LiveLog liveLog : liveLogs) {
                if (Objects.equals(liveLog.getShopId(), shopId)) {
                    shopNum += liveLog.getNum();
                }
                platformNum += liveLog.getNum();
            }
        }
        liveUsableNumParam.setShopNum(type.getShopNumLimit() - shopNum);
        liveUsableNumParam.setTotalNum(type.getNumLimit() - platformNum);
        return liveUsableNumParam;
    }

    /**
     * 校验商家和平台可用次数并保存日志
     * @param shopId 店铺id
     * @param type 校验类型
     */
    @Override
    public void checkNumsAndSaveLog(Long shopId, LiveInterfaceType type, String desc) {
        LiveLogDTO liveLogDTO = new LiveLogDTO();
        liveLogDTO.setShopId(shopId);
        liveLogDTO.setStartTime(DateUtil.beginOfDay(new Date()));
        liveLogDTO.setEndTime(DateUtil.endOfDay(new Date()));
        liveLogDTO.setType(type.value());
        List<LiveLog> liveLogs = liveLogMapper.list(liveLogDTO);
        int platformNum = 0;
        int shopNum = 0;
        LiveLog liveLogDb = null;
        if(CollectionUtils.isNotEmpty(liveLogs)) {
            for (LiveLog liveLog : liveLogs) {
                if (Objects.equals(liveLog.getShopId(),shopId)) {
                    shopNum = liveLog.getNum();
                    liveLogDb = liveLog;
                }
                platformNum += liveLog.getNum();
            }
        }
        // 如果次数大于平台每日上限则抛出异常
        if(platformNum + 1 > type.getNumLimit()){
            throw new LuckException("平台今日可用次数已达上限!");
        }

        // 校验商家今日可用次数
        if(shopNum + 1 > type.getShopNumLimit()){
            throw new LuckException("店铺今日可用次数已达上限!");
        }

        // 保存or修改日志
        if(Objects.isNull(liveLogDb)){
            LiveLog liveLog = new LiveLog();
            liveLog.setCreateTime(new Date());
            liveLog.setNum(1);
            liveLog.setShopId(shopId);
            liveLog.setType(type.value());
            save(liveLog);
        }else{
            liveLogDb.setNum(liveLogDb.getNum() + 1);
            update(liveLogDb);
        }
    }

    @Override
    public LiveLog getLiveLogByParams(LiveLogDTO liveLogDTO) {
        return liveLogMapper.getLiveLogByParams(liveLogDTO);
    }
}
